//
//  ZHTabBarController.m
//  黑马微博
//
//  Created by wangzhaohui-Mac on 14-7-3.
//  Copyright (c) 2014年 com.Itheima. All rights reserved.
//

#import "WKTabBarController.h"
#import "WKNavigationController.h"
#import "WKTabBar.h"
#import "WKOneViewController.h"
#import "WKTwoViewController.h"
#import "WKThreeViewController.h"
#import "WKFourViewController.h"
#import "WKMeViewController.h"

@interface WKTabBarController () <WKTabBarDelegate,UITabBarControllerDelegate>
/**最后选择的控制器*/
@property (nonatomic, weak) UIViewController *lastSelectedViewContoller;
/**首页*/
@property (nonatomic, weak) WKOneViewController *home;
@end

@implementation WKTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加所有的子控制器
    [self addAllChildViewController];
    
    //添加自定义tabBar
//    [self addTabBar];
    
    [self setupNavigationBarButtonItem];
   
}
/**
 *  创建自定义tabBar
 */
- (void)addTabBar
{
    WKTabBar *tabBar = [[WKTabBar alloc] init];
    tabBar.WKtabBarDelegate = self;
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
    [self setValue:tabBar forKey:@"tabBar"];
    
}


- (void)setupNavigationBarButtonItem
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonPublish" highlightImage:@"navigationButtonPublishClick" target:self action:@selector(compile)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonRefresh" highlightImage:@"navigationButtonRefreshClick" target:self action:@selector(refresh)];
}

- (void)compile
{
    WKLog(@"compile--->>");
    
}

- (void)refresh
{
    WKLog(@"refresh--->>");
    
}

#pragma mark -UITabBarControllerDelegate代理方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController
{
    UIViewController *vc = [viewController.viewControllers firstObject];
    if ([vc isKindOfClass:[WKOneViewController class]]) {
        if (self.lastSelectedViewContoller == vc) {
            [self.home refresh:YES];
        }else
        {
            [self.home refresh:NO];
        }
    }
    self.lastSelectedViewContoller = vc;
    
}


- (void)tabBarDidClickedPlusButton:(WKTabBar *)tabBar
{
    WKLog(@"tabBarDidClickedPlusButton--->>");

}


/**
 *  添加所有子控制器
 */
- (void)addAllChildViewController
{
    WKOneViewController *oneVc = [[WKOneViewController alloc] init];
    self.home = oneVc;
    [self addChildViewController:oneVc title:@"图片" imageName:@"tabbarQuotation" selectedImageName:@"tabbarQuotationClick"];
    
    WKTwoViewController *towVc = [[WKTwoViewController alloc] init];
    [self addChildViewController:towVc title:@"段子" imageName:@"tabbarEssay" selectedImageName:@"tabbarEssayClick"];

    WKThreeViewController *therrVc = [[WKThreeViewController alloc] init];
    [self addChildViewController:therrVc title:@"声音" imageName:@"tabbarVoice"selectedImageName:@"tabbarVoiceClick"];
    
    WKFourViewController *fourVc = [[WKFourViewController alloc] init];
    [self addChildViewController:fourVc title:@"视频" imageName:@"tabbarVideo"selectedImageName:@"tabbarVideoClick"];
    
    WKMeViewController *myVc = [[WKMeViewController alloc] init];
//    myVc.view.backgroundColor = WKRandomColor;
    [self addChildViewController:myVc title:@"我的" imageName:@"tabbarSetup" selectedImageName:@"tabbarSetupClick"];
    

}

/**
 *  添加一个子控制器
 *  @param childVc           子控制器
 *  @param title             标题
 *  @param imageName         默认状态图片名称
 *  @param selectedImageName 选中状态图片的名称
 */
- (void)addChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    
    //同时设置tabBarItem和导航控制器的标题
//    childVc.title = title;
    childVc.tabBarItem.title = title;

    //设置普通状态下tabBarItem的字体属性
    NSMutableDictionary *nolmalDict = [NSMutableDictionary dictionary];
    nolmalDict[UITextAttributeTextColor] = [UIColor grayColor];
    nolmalDict[UITextAttributeFont] = [UIFont systemFontOfSize:10];
    [childVc.tabBarItem setTitleTextAttributes:nolmalDict forState:UIControlStateNormal];
    
    //设置选择状态下tabBarItem的字体属性
    NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];
    selectedDict[UITextAttributeTextColor] = WKColor(255, 30, 28);
    [childVc.tabBarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];
    
    
    //设置tabBarItem普通状态下的图片
    childVc.tabBarItem.image = [UIImage imageWithName:imageName];
    
    //设置tabBarItem选中状态下的图片
    UIImage *selectedImage = [UIImage imageWithName:selectedImageName];
    //设置在iOS7下选中的图片不被渲染
    if (iOS7) {
        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    childVc.tabBarItem.selectedImage = selectedImage;
    
    //给子控制器包装一个导航控制器
    WKNavigationController *nvc = [[WKNavigationController alloc] initWithRootViewController:childVc];
    
    //将导航控制器添加到ZHTabBarController中
    [self addChildViewController:nvc];
    
}


@end
